<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: DataSource.Get</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: DataSource.Get</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><style scoped>
/* custom styles for this example */
#demo .output {margin-bottom:1em; padding:10px; border:1px solid #D9D9D9;}
</style>

<div class="intro">
    <p>DataSource.Get uses the Get Utility to retrieve data, even cross-domain resources, via a dynamically created script node. A <a href="../dataschema/">DataSchema</a> plugin is used to normalize incoming data into a known format for consistency of usage by other components. Please note that your data resource must support a callback mechanism, which is a function wrapper around the returned data. The name of the callback function is passed to the resource via a query string parameter defined by the DataSource.Get attribute <code>scriptCallbackParam</code>.</p>
</div>

<div class="example yui3-skin-sam">
    <form id="demo">
    <h4>JSON</h4>
    <h6>Data</h6>
    <pre>
{
    "query":{
        "count":"10",
        "created":"2011-06-15T04:36:13Z",
        "lang":"en-US",
        "results":{
            "event":[
                {
                 "id": "6826736",
                 "name": "PGA Fall Expo 2011",
                 "start_date": "2011-08-01",
                 "venue_name": "The Venetian Congress and Sands Expo Convention Center",
                 ...
                },
                {
                 "id": "7228592",
                 "name": "NAHC 30th Annual Meeting & Exposition",
                 "start_date": "2011-10-01",
                 "venue_name": "Mandalay Bay Resort",
                 ...
                },
                {
                 "id": "7238009",
                 "name": "Water and Society 2011-First International Conference on Water and Society",
                 "start_date": "2011-12-05",
                 "venue_name": "Richard Tam Alumni Center",
                 ...
                },
                ...
            ]
        }
    }
}
    </pre>

    <h6>Schema</h6>
    <pre>
{
    resultListLocator: "query.results.event",
    resultFields: ["name"]
}
    </pre>

    <h6>Normalized data</h6>
    <input type="button" id="demo_json" value="Retrieve data">
    <div id="demo_output_json" class="output"></div>
</form>

<script type="text/javascript">
YUI().use("json-stringify", "node", "datasource-get", "datasource-jsonschema", function (Y) {
    var myDataSource = new Y.DataSource.Get({
            source: "http://query.yahooapis.com/v1/public/yql?format=json&"
        });

    myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
        schema: {
            resultListLocator: "query.results.event",
            resultFields: ["name"]
        }
    });

    Y.on("click", function(e){
        // See what's happening in Las Vegas (woeid 2436704)
        myDataSource.sendRequest({
            request: "q=select%20*%20from%20upcoming.events.bestinplace%20where%20woeid=2436704",
            callback: {
                success: function(e){
                    Y.one("#demo_output_json")
                        .setContent("<pre>" +
                            Y.JSON.stringify(e.response, null, 4)
                                .replace(/&/g,"&amp;")
                                .replace(/</g,"&lt;")
                                .replace(/>/g,"&gt;") +
                                "</pre>");
                },
                failure: function(e){
                    Y.one("#demo_output_json")
                        .setContent("Could not retrieve data: " + e.error.message);
                }
            }
        });
    }, "#demo_json");
});
</script>


</div>

<p>Use a DataSourceJSONSchema plugin to parse the data against a schema that you provide:</p>

<pre class="code prettyprint">YUI().use(&quot;datasource-get&quot;, &quot;datasource-jsonschema&quot;, function(Y) {
    var myDataSource = new Y.DataSource.Get({
            source: &quot;http:&#x2F;&#x2F;query.yahooapis.com&#x2F;v1&#x2F;public&#x2F;yql?format=json&amp;&quot;
        });

    myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
        schema: {
            resultListLocator: &quot;query.results.result&quot;,
            resultFields: [&quot;title&quot;]
        }
    });

    myDataSource.sendRequest({
        request: &quot;q=select%20*%20from%20upcoming.events.bestinplace...&quot;,
        callback: {
            success: function (e) { &#x2F;* output to screen *&#x2F; },
            failure: function (e) { &#x2F;* output to screen *&#x2F; }
        }
    });

});</pre>


</div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="The Local DataSource manages retrieval of in-page data, from JavaScript arrays and objects to DOM elements.">
                                        <a href="datasource-local.html">DataSource.Local</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The Get DataSource, which manages retrieval of data from remote sources via the Get Utility, can be useful for accessing data from cross-domain servers without the need for a proxy.">
                                        <a href="datasource-get.html">DataSource.Get</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The IO DataSource manages retrieval of data from remote sources, via the IO Utility.">
                                        <a href="datasource-io.html">DataSource.IO</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The Function DataSource, which manages retrieval of data from a JavaScript function, provides a highly customizeable mechanism for implementer-defined data retrieval algorithms">
                                        <a href="datasource-function.html">DataSource.Function</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources.">
                                        <a href="datasource-caching.html">DataSource with Caching</a>
                                    </li>
                                
                            
                                
                                    <li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions.">
                                        <a href="datasource-offlinecache.html">DataSource with Offline Cache</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Use the Pollable extension to enable polling in your DataSource.">
                                        <a href="datasource-polling.html">DataSource with Polling</a>
                                    </li>
                                
                            
                                
                            
                        </ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples That Use This Component</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                            
                                
                                    <li data-description="How to provide autocomplete suggestions using a DataSource instance.">
                                        <a href="../autocomplete/ac-datasource.html">Remote Data via DataSource</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
